$(document).ready(function(){
	$('#aftersubmit').hide();
});

function submitForm()
{
	try{
//	alert("got here");
	var userid = document.forms["register"]["username"].value;
	var pw = document.forms["register"]["password"].value;
	var pw2 = document.forms["register"]["password2"].value;
	var email = document.forms["register"]["email"].value;
	var first = document.forms["register"]["firstname"].value;
	var last = document.forms["register"]["lastname"].value;
	var location = document.forms["register"]["location"].value;
	var genderdrop = document.getElementById('gender');
	var gender = genderdrop.options[genderdrop.selectedIndex].value;
	var bio = document.forms["register"]["bio"].value;
	
	if (userid == null || userid == "") {
		alert("Please enter a username.");
		return false;
	}
	if (pw == null || pw == "") {
		alert("Please enter a password.");
		return false;
	}
	if (pw2 == null || pw2 == "") {
		alert("Please confirm your password.");
		return false;
	}
	if (pw != pw2) {
		alert("Your passwords don't match. Please enter them again.");
		return false;
	}
	var atpos = email.indexOf("@");
	var dotpos = email.lastIndexOf(".");
	if (atpos < 1 || dotpos < atpos+2 || dotpos+2 >= email.length) {
		alert("Please enter a valid e-mail address.");
		return false;		
	}
	$('#aftersubmit').show();
	if (gender == "SELECT") {
//		alert("Please select a gender.");
		gender = "NULL";
	}
//	alert("2222got here");
	var url = "/writeonproject/create?object=user&username=" + userid
	+ "&password=" + pw + "&email=" + email;

	if (first != null || first != "") {
		url = url + "&firstname=" + first;
	}

	if (last != null || last != "") {
		url = url + "&lastname=" + last;
	}
	if (location != null || location != "") {
		url = url + "&location=" + location;
	}
	
	if (gender != null || gender != "") {
		url = url + "&gender=" + gender;
	}
	if (bio != null || bio != "") {
		url = url + "&bio=" + bio;
	}
	
	$.getJSON(url, registerCallBack);

//	alert("33got here");
	}catch(e){alert(e.message);}
}


function registerCallBack(data) {
//	if( data.error != null ) {
//		alert(data.reason);
//	}
	if( data.error == "false" ) {
		window.location = "/writeonproject/home?page=register_success";
	} else {
		alert("Error! : " + data.reason );
	}
}


	